const public_url = "https://hncsqysm.imwork.net/api/"
const picurl = "https://hncsqysm.imwork.net"
const videourl = ""
// 资讯中的图片地址前缀
const newsurl = ""
const pre_url = ""


const toReloginPage = function(message) {
  uni.showToast({
    icon: 'none',
    title: message,
    duration: 1000
  })
  uni.clearStorageSync()

}


const fetch = {

  toast: function(tips) {
    uni.showToast({
      title: tips || "出错啦~",
      icon: 'none',
      duration: 2000
    })
  },
  /**
   * 根据token 获取用户
   * @param {Object} 
   */
  loginByToken: function(token) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: public_url + pre_url + "wx/loginByToken",
        data: {
          token: token
        },
        method: "GET",
        success: function(res) {
          console.log(res.data.code)
          if (res.data.code == 200) {
            //登录成功
            resolve();
          } else {
            //登录失败
            toReloginPage(res.data.message);
            reject(res.data);
          }
        },
        fail() {
          uni.showToast({
            title: '网络连接失败',
            icon: 'none',
            duration: 2000
          });
        }
      })
    })
  },
  request: function(url, postData, method, type, showLoading, loadTitle) {
    return new Promise((resolve, reject) => {
      let user = uni.getStorageSync('user')
      //接口请求
      if (showLoading) {
        uni.showLoading({
          mask: true,
          title: loadTitle ? loadTitle : '请稍候...'
        })
      }
      // const token = postData.token || "";

      //本地获取token
      // let _token = uni.getStorageSync('token')
      // if (_token == null || _token == '') {

      //   //放行登录操作
      //   if (url != 'wx/login' && url != 'https://apis.map.qq.com/ws/geocoder/v1/') {
      //     toReloginPage();
      //     reject();
      //     return;
      //   }
      // }
      if (method == 'post' || method == 'delete' || method == 'put') {
        postData = JSON.stringify(postData)
        type = "application/json"
      }

      if (!url.startsWith('http')) {
        url = public_url + pre_url + url
      }

      let _header = {
        'content-type': type ? type : 'application/x-www-form-urlencoded',
        // 'X-Token': _token
      }
      if (user) {
        _header['X-USER-ID'] = user.phone
      }
      uni.request({
        url: url,
        data: postData,
        header: _header,
        method: method, //'GET','POST'
        dataType: 'json',
        success: (res) => {
          // console.group()
          // console.log(postData)
          // console.log(url)
          // console.log(res)
          // console.groupEnd()
          uni.hideLoading()

          if (res.data.code && res.data.code.code == 1) {
            reject(res.data);
          } else {
            resolve(res.data);
          }
        },
        fail: (res) => {
          fetch.toast("网络不给力，请稍后再试~")
          // reject(res)
        }
      })
    })
  },
  //验证登录状态
  varifyLogin: function() {
    uni.getStorageSync('')
  }
}



/**
 * 根据字典id获取字典值列表
 * @param {Object} id 字典id
 */
const initDict = function(id) {
   return new Promise((resolve, reject) => {
    fetch.request("dict/getItems/" + id).then(re => {
      resolve(re)
    }).catch(re => {
      console.log('初始化字典错误')
    })
  })
}



module.exports = {
  request: fetch.request,
  toast: fetch.toast,
  loginByToken: fetch.loginByToken,
  picurl: picurl,
  initDict: initDict,
  appurl: public_url,
  videourl: videourl,
  newsurl: newsurl
}
